﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHControlLib;
using thinger.MTHDLL;
using thinger.MTHModels;

namespace thinger.MTHProject
{
    public partial class FrmHistory : Form
    {
        public FrmHistory()
        {
            InitializeComponent();

            this.dtpStart.Value = DateTime.Now.AddDays(-1f);
            this.dtpEnd.Value = DateTime.Now;

            this.Load += FrmHistory_Load;
        }
        private Dictionary<string, string> nameDictionary = new Dictionary<string, string>();

        private ActualDataManage actualDataManage = new ActualDataManage();
        private void FrmHistory_Load(object sender, EventArgs e)
        {

            foreach (var item in this.CheckPanel.Controls.OfType<CheckBoxEx>())
            {
                if (item.Tag != null && item.ToString().Trim().Length > 0)
                {
                    if (item.Checked) 
                    {
                        nameDictionary.Add(item.Tag.ToString().Trim(), item.Text.ToString().Trim());
                    }
                    
                }
            }

            //设置X轴的数据类型以及格式
            this.ChartTH.XDataType = SeeSharpTools.JY.GUI.StripChartX.XAxisDataType.TimeStamp;
            
            //设置图例
            this.ChartTH.LegendVisible = true;
            //设置显示数据点
            this.ChartTH.DisplayPoints = 10000;
            //Y轴范围
            this.ChartTH.AxisX.Minimum = 0.0f;
            this.ChartTH.AxisX.Maximum = 100.0f;
            this.ChartTH.AxisX.AutoScale = false;//设置x轴成非自适应

        }



        private void btnByTime_Click(object sender, EventArgs e)
        {
            string start = this.dtpStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string end = this.dtpEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");
            //校验
            if (Convert.ToDateTime(end) < Convert.ToDateTime(start))
            {
                new FrmMsgBoxWithOutAck("结束时间不能小于开始时间", "请检查").Show();
            }
            TimeSpan timeSpan = Convert.ToDateTime(end) - Convert.ToDateTime(start);

            double totalDays = timeSpan.TotalDays;

            if (totalDays > 1)
            {
                new FrmMsgBoxWithOutAck("查询时间不能超过1天", "请检查").Show();
            }
            Task<DataTable> task = Task.Run(() =>
            {
                DataTable dataTable = actualDataManage.ActualByTimeTable(start, end);
                return dataTable;
            });
            task.ContinueWith(task1 =>
            {
                this.Invoke(new Action(() =>
                {
                    
                    if (task1.Result.Rows.Count > 0)
                    {
                        Refresh(task1.Result);
                    }

                }));
                

            });


        }
        //刷新
        private void Refresh(DataTable dataTable)
        {
            this.ChartTH.Clear();
            int rows = dataTable.Rows.Count;
            Double[,] YData = new Double[nameDictionary.Count, rows];
            string[] XData = new string[rows];

            this.ChartTH.SeriesCount = nameDictionary.Count;

            for (int i = 0; i < nameDictionary.Count; i++)
            {
                this.ChartTH.Series[i].Name = nameDictionary.Values.ToList()[i];
                this.ChartTH.Series[i].Width = SeeSharpTools.JY.GUI.StripChartXSeries.LineWidth.Middle;
            }

            for (int i = 0; i < rows; i++)
            {
                XData[i] = dataTable.Rows[i][0].ToString();

                for (int j = 0; j < nameDictionary.Count; j++)
                {
                    YData[j, i] = Convert.ToDouble(dataTable.Rows[i][j + 1]);
                }

            };

            this.ChartTH.Plot(YData,XData);
        }

        private void btnFast_Click(object sender, EventArgs e)
        {
            this.dtpStart.Value = DateTime.Now.AddHours(-2f);
            this.dtpEnd.Value = DateTime.Now;
            this.btnByTime_Click(null,null);
        }

        private void btnAddIMG_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //筛选文件
            saveFileDialog.Filter = "图片(*.jpg)|*.jpg|所有文件|*.*";

            saveFileDialog.Title = "导出历史趋势图片";
            //扩展名
            saveFileDialog.DefaultExt = "jpg";

            saveFileDialog.FileName = "历史趋势图片" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //是否使用扩展名字
            saveFileDialog.AddExtension = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //保存
                this.ChartTH.SaveAsImage(saveFileDialog.FileName);

                DialogResult dialogResult = new FrmMsgBoxWithAck("导出完成", "是否打开文件").ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    //打开文件
                    Process.Start(saveFileDialog.FileName);
                }
            }
        }

        private void btnAddCSV_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //筛选文件
            saveFileDialog.Filter = "文件CSV(*.csv)|*.csv|所有文件|*.*";

            saveFileDialog.Title = "导出历史趋势CSV";
            //扩展名
            saveFileDialog.DefaultExt = "csv";

            saveFileDialog.FileName = "历史趋势CSV" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //是否使用扩展名字
            saveFileDialog.AddExtension = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                //保存
                this.ChartTH.SaveAsCsv(saveFileDialog.FileName);

                DialogResult dialogResult = new FrmMsgBoxWithAck("导出完成", "是否打开文件").ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    //打开文件
                    Process.Start(saveFileDialog.FileName);
                }
            }
        }
    }
}
